package com.qifn.blog.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qifn.blog.Dao.BlogDao;
import com.qifn.blog.Model.Blog;
import com.qifn.blog.Model.Tag;
import com.qifn.blog.Service.BlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    BlogDao blogDao;

    @Override
    public PageInfo<Blog> adminIndexQueryBlog(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogDao.adminIndexQueryAllBlog();
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList);
        return blogPageInfo;
    }

    @Override
    public PageInfo<Blog> adminIndexSearchBlog(String title,String typeId,String recommend,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        if(recommend.equals("on")){
            recommend = "1";
        }else {
            recommend = "0";
        }
        List<Blog> blogList = blogDao.adminIndexSearchBlog(title,typeId,recommend);
        return new PageInfo<>(blogList);
    }

    @Override
    public boolean adminReleaseBlog(Blog blog,List<Tag> tagIdList) {
        Integer one = blogDao.adminReleaseInsertBlog(blog);
        Integer two = blogDao.adminReleaseInsertBlog_Tag(blog.getId(),blog.getTags());
        if(one > 0 && two > 0){
            return true;
        }
        return false;
    }

    @Override
    public Blog adminUpdateBlog(Integer blogId) {
        return blogDao.adminUpdateQueryBlog(blogId);
    }

    @Override
    public boolean adminReleaseUpdateBlog(Blog blog) {
        Integer one = blogDao.adminDeleteBlg_TagByBlogId(blog.getId());  //删除原有的一对多关系
        Integer two = blogDao.adminReleaseUpdateBlog(blog);
        Integer three = blogDao.adminUpdateInsertBlog_Tag(blog.getId(),blog.getTags());  //重新建立一对多关系
        if(one > 0 && two > 0 && three > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean adminIndexDeleteBlog(Integer blogId) {
        Integer one = blogDao.adminIndexDeleteBlog(blogId);
        if(one > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<Blog> indexQueryBlog(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogDao.indexQueryBlog();
        for (Blog blog:blogList) {
            blog.setContent(blog.getContent().substring(0,72) + "...");
        }
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return pageInfo;
    }

    @Override
    public List<Blog> indexRecommendBlog() {
        return blogDao.indexQueryRecommendBlog();
    }

    @Override
    public PageInfo<Blog> queryBlogByTypeId(Integer typeId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogDao.queryBlogByTypeId(typeId);
        for (Blog blog:blogList) {
            blog.setContent(blog.getContent().substring(0,72) + "...");
        }
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList);
        return blogPageInfo;
    }

    @Override
    public PageInfo<Blog> queryBlogByTagId(Integer tagId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogDao.queryBlogByTagId(tagId);
        for (Blog blog:blogList) {
            blog.setContent(blog.getContent().substring(0,72) + "...");
        }
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList);
        return blogPageInfo;
    }

    @Override
    public Blog queryBlogByBlogId(Integer blogId) {
        return blogDao.queryBlogByBlogId(blogId);
    }

    @Override
    public PageInfo<Blog> indexSearchBlog(Integer pageNum,Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Blog> blogList = blogDao.indexSearchBlog(keyword);
        for (Blog blog:blogList) {
            blog.setContent(blog.getContent().substring(0,72) + "...");
        }
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList);
        return blogPageInfo;
    }
}
